package com.yunji.cabinet.business;

import com.yunji.cabinet.bo.CapacityQuantityBO;
import com.yunji.cabinet.bo.FillCargoQueryBO;
import com.yunji.cabinet.cache.CabinetCache;
import com.yunji.cabinet.context.calculate.CabinetCalculate;
import com.yunji.cabinet.domain.MaintainDO;
import com.yunji.cabinet.params.RecordParam;
import com.yunji.cabinet.service.ITakeDeliveryService;
import com.yunji.cabinet.service.impl.DeviceService;
import com.yunji.cabinet.service.impl.FillCargoInfoService;
import com.yunji.cabinet.service.impl.MaintainService;
import com.yunji.cabinet.vo.MaintainDetailVO;
import com.yunji.cabinet.vo.MaintainVO;
import com.yunji.cabinet.vo.TakeDeliveryVO;
import com.yunji.common.utils.ConvertDataUtil;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.coffee.entity.bo.CommonDeviceInfoBO;
import com.yunji.coffee.entity.dto.ICommonDeviceDetail;
import com.yunji.coffee.entity.dto.ICommonDeviceInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.cabinet.business
 * @Title: MaintainBusiness
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-11-01 15:18:55      V1.0        HuaAo       新建类
 */
@Component
public class MaintainBusiness {

    @Resource
    MaintainService maintainService;

    @Resource
    FillCargoInfoService cargoInfoService;

    @Resource
    DeviceService deviceService;

    @Resource
    ITakeDeliveryService takeDeliveryService;

    public List<MaintainVO> searchPageList(RecordParam param) {
        List<MaintainVO> voList = maintainService.searchPageList(param);
        for (MaintainVO maintainVO : voList) {
            CommonDeviceInfoBO infoBO = deviceService.getDeviceInfoBO(maintainVO.getMaintainCode());
            if (BeanUtils.isNotNull(infoBO)) {
                ICommonDeviceInfo beforeDeviceInfo = infoBO.getBeforeDeviceInfo();
                if (BeanUtils.isNotNull(beforeDeviceInfo)) {
                    maintainVO.setStartVolt(beforeDeviceInfo.getBattleLevel());
                }
                ICommonDeviceInfo afterDeviceInfo = infoBO.getAfterDeviceInfo();
                if (BeanUtils.isNotNull(afterDeviceInfo)) {
                    maintainVO.setCloseVolt(afterDeviceInfo.getBattleLevel());
                }
            }
        }
        return voList;
    }

    public List<TakeDeliveryVO> takeDeliveryLIst(RecordParam param) {
        return takeDeliveryService.searchPageList(param);
    }

    public List<MaintainDetailVO> getMaintainInfo(String maintainCode) {
        List<MaintainDetailVO> infoDOList = cargoInfoService.search(FillCargoQueryBO.builder().maintainCode(maintainCode).build());
        if (BeanUtils.isNull(infoDOList) ) return new ArrayList<>();
        MaintainDO param = new MaintainDO();
        param.setMaintainCode(maintainCode);
        MaintainDO maintainDO = maintainService.select(param);
        Map<Integer, MaintainDetailVO> map = ConvertDataUtil.list2Map(infoDOList, MaintainDetailVO::getOrdinal);
        CommonDeviceInfoBO infoBO = deviceService.getDeviceInfoBO(maintainCode);
        if (BeanUtils.isNotNull(infoBO)) {
            ICommonDeviceInfo beforeDeviceInfo = infoBO.getBeforeDeviceInfo();
            if (BeanUtils.isNotNull(beforeDeviceInfo)) {
                for (ICommonDeviceDetail detail : beforeDeviceInfo.getDetailList()) {
                    MaintainDetailVO vo = map.get(detail.getOrdinal());
                    vo.setStartBattleLevel(beforeDeviceInfo.getBattleLevel());
                    vo.setStartHeight(detail.getDistance());
                    vo.setStartNums(CabinetCalculate.getQuantityByHeightDecimal(
                            CapacityQuantityBO.builder().distance(CabinetCache.getTypeHeight(maintainDO.getTypeCode()).intValue() - detail.getDistance()).productId(vo.getProductId()).build()).intValue());
                }
            }
            ICommonDeviceInfo afterDeviceInfo = infoBO.getAfterDeviceInfo();
            if (BeanUtils.isNotNull(afterDeviceInfo)) {
                for (ICommonDeviceDetail detail : afterDeviceInfo.getDetailList()) {
                    MaintainDetailVO vo = map.get(detail.getOrdinal());
                    vo.setCloseBattleLevel(beforeDeviceInfo.getBattleLevel());
                    vo.setCloseHeight(detail.getDistance());
                    vo.setCloseNums(CabinetCalculate.getQuantityByHeightDecimal(
                            CapacityQuantityBO.builder().distance(CabinetCache.getTypeHeight(maintainDO.getTypeCode()).intValue() - detail.getDistance()).productId(vo.getProductId()).build()).intValue());
                }
            }
        }
        return infoDOList;
    }
}